/*常见弹窗
*    标题
*    内容
* 取消  确定*/
import 'package:flutter/material.dart';
import 'package:frametrim/utility/screen.dart';
import 'package:frametrim/utility/sq_color.dart';

class CommonDialog extends Dialog {
  String? title;
  String content;
  String? button_left;
  Color button_leftColor;
  String? button_right;
  Color button_rightColor;
  VoidCallback onPress;

  CommonDialog(
      {Key? key,
      this.title,
      required this.content,
      this.button_left='取消',
      this.button_leftColor= const Color(0xE6000000),
      this.button_right='确定',
      this.button_rightColor=const Color(0xFF0052D9),
      required this.onPress});

  double buttonWidth = (Screen.width - 60 - 1) / 2;

  @override
  Widget build(BuildContext context) {
    //创建透明图层
    return Padding(
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Material(
        //设置图层透明
        type: MaterialType.transparency,
        //这里要使用Column包裹限制高度，否则弹窗会和屏幕一眼高
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              decoration: ShapeDecoration(
                  color: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(8)))),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                    child: Column(
                      children: <Widget>[
                        SizedBox(
                          height: 10,
                        ),
                        Text(
                          title!,
                          style: TextStyle(fontSize: 18, color: Colors.black),
                        ),
                        SizedBox(
                          height: 20,
                        ),
                        Text(
                          content,
                          textAlign: TextAlign.center,
                          style:
                              TextStyle(color: Color(0xff888888), fontSize: 15),
                        ),
                        SizedBox(
                          height: 10,
                        ),
                      ],
                    ),
                  ),
                  Divider(
                    height: 1,
                    color: Color(0xffE5E5E5),
                  ),
                  Container(
                    child: Row(
                      children:buildAddButton(context)
                    ),
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  List<Widget> buildAddButton(BuildContext context) {
    List<Widget> children = [];
    if (button_left != null) {
      children.add(buildButtonView('取消', () {
        Navigator.pop(context);
      }, button_leftColor));
      /*VerticalDivider 要用Container包裹设置高度，否则默认高度为0*/
      children.add(Container(
        height:48,
        child: VerticalDivider(width: 1,color: Color(0xffE5E5E5)),
      ));
    }
    children.add(buildButtonView(button_right!, onPress, button_rightColor));
    return children;
  }

  Widget buildButtonView(String name, VoidCallback callback, Color mcolor) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: callback,
      child: Container(
        width: buttonWidth,
        height: 48,
        child: Center(
          child: Text(
            name,
            style: TextStyle(fontSize: 17, color: mcolor),
          ),
        ),
      ),
    );
  }
}
